<template>
  <div class="home_content">
    <!-- 提示 -->
    <div class="content">
      <p class="title">同学你好！当前日期为</p>
      <p class="date">
        {{
          `${new Date().getFullYear()}-${
            new Date().getMonth() + 1
          }-${new Date().getDate()}
          `
        }}
      </p>
      <p class="tip">点击下方「记账」记录最近的开销吧！~</p>
    </div>

    <!-- 列表 -->
    <section class="lists">
      <div class="tallyData_lists" v-if="tallyData.length > 0">
        <ul>
          <li v-for="(item, index) in tallyData" :key="index">
            <el-row class="tallyData_item">
              <el-col class="tag">{{ item.tag }}</el-col>
              <el-col class="remark">{{ item.remark }}</el-col>
              <el-col :class="['price', item.type === 0 ? 'expense' : 'income']"
                >￥{{ item.price }}
              </el-col>

              <el-col class="time">{{ item.createdTime | filterDate }}</el-col>
              <el-col>
                <div class="icons">
                  <i
                    class="el-icon-delete delete"
                    @click="deleteTag(item, index)"
                  ></i>
                </div>
              </el-col>
            </el-row>
          </li>
        </ul>
      </div>
      <p class="noData" v-else>暂无数据</p>
    </section>
  </div>
</template>

<script>
import { mapGetters } from "vuex"
import { filterDate } from "@/filters/filterdate.js"
export default {
  data() {
    return {
      // 获取vuex中的记账数据，只要前10条
      // tallyData: this.$store.getters.getTallyData.slice(0, 10),
      // 获取vuex中的所有记账数据
      tallyData: this.$store.getters.getTallyData,
    }
  },
  filters: {
    //毫秒格式化为日期
    filterDate,
  },
  computed: {
    ...mapGetters(["getTallyData"]),
    ...mapGetters(["getTallyDataFromServer"]),
  },
  methods: {
    deleteTag: function (item) {
      let id = item.id
      // let str = window.localStorage.getItem("tallyData")
      // let tallyData = JSON.parse(str)
      // tallyData.splice(index, 1)
      this.$store.dispatch("deleteTallyDataFromServer", id)

      //删除LocalStorage中某一条数据后，进行当前组件刷新
      //this.$router.go(0)
    },
  },
  //  方法二： 利用watch动态侦听state的变化，实时改变页面的数据
  watch: {
    getTallyData: function (data) {
      // console.log(data)
      // console.log(this.$route)
      this.tallyData = data
    },
  },
  created() {
    this.$store.dispatch("getTallyDataFromServer")
  },
}
</script>

<style lang="scss" scoped>
.common_content {
  background-color: #28fc79;
  color: #fff;
  padding: 13px 15px;
}
.home_content {
  // 提示内容
  .content {
    background-color: #28fc79;
    color: #fff;
    padding: 13px 15px;
    .date,
    .tip {
      text-align: center;
    }
    .date {
      padding: 30px 0;
      font-size: 35px;
      word-spacing: 0;
    }
    .tip {
      padding-bottom: 15px;
    }
  }

  //   列表
  .lists {
    .lists_header {
      display: flex;
      align-items: center;
      padding: 15px 15px;
      .icon {
        font-size: 32px;
      }
      .recently {
        padding-left: 5px;
        font-weight: 700;
      }
    }
    .noData {
      margin-top: 10px;
      text-align: center;
      font-size: 20px;
      opacity: 0.5;
    }
  }
}

.tallyData_lists {
  .tallyData_item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
    padding: 6px 8px;
    background-color: #fff;
    border-radius: 5px;
    .remark {
      font-size: 14px;
      color: #000;
      opacity: 0.5;
    }
    .price {
      word-break: break-all;
    }
    .time {
      text-align: right;
      font-size: 15px;
      color: #000;
      opacity: 0.5;
    }
    .expense {
      color: red;
    }
    .income {
      color: skyblue;
    }
    .icons {
      display: flex;
      justify-content: center;
      align-items: center;
      .delete {
        width: 16px;
        height: 16px;
        color: red;
      }
    }
  }
}
</style>
